#!/bin/sh

VERSION="$(cat ../version)"
TARBALL_FILE="stafflog-services-$VERSION.tar.gz"
TARBALL_ORIG_FILE="stafflog-services_$VERSION.orig.tar.gz"
PACKAGE_FILE="*_all.deb"

TMP_DIR="tmp"
OUT_DIR="output"

DEBIAN_DIR="debian"

TARBALL_DIR="stafflog-services-$VERSION"
SRC_DEBIAN_DIR="debian"

rm -f $OUT_DIR/stafflog-services_*.deb
rm -rf $TMP_DIR

sh generate-tarball.sh

mkdir $TMP_DIR
cp $OUT_DIR/$TARBALL_FILE $TMP_DIR
cp $TMP_DIR/$TARBALL_FILE $TMP_DIR/$TARBALL_ORIG_FILE

cd $TMP_DIR
tar xzvf $TARBALL_FILE
cd ..

mkdir -p $TMP_DIR/$TARBALL_DIR/$SRC_DEBIAN_DIR

cp -r $DEBIAN_DIR/source $TMP_DIR/$TARBALL_DIR/$SRC_DEBIAN_DIR
cp $DEBIAN_DIR/changelog $TMP_DIR/$TARBALL_DIR/$SRC_DEBIAN_DIR
cp $DEBIAN_DIR/compat $TMP_DIR/$TARBALL_DIR/$SRC_DEBIAN_DIR
cp $DEBIAN_DIR/control $TMP_DIR/$TARBALL_DIR/$SRC_DEBIAN_DIR
cp $DEBIAN_DIR/copyright $TMP_DIR/$TARBALL_DIR/$SRC_DEBIAN_DIR
cp $DEBIAN_DIR/postinst $TMP_DIR/$TARBALL_DIR/$SRC_DEBIAN_DIR
cp $DEBIAN_DIR/prerm $TMP_DIR/$TARBALL_DIR/$SRC_DEBIAN_DIR
cp $DEBIAN_DIR/rules $TMP_DIR/$TARBALL_DIR/$SRC_DEBIAN_DIR

cp $DEBIAN_DIR/Makefile $TMP_DIR/$TARBALL_DIR

cp $DEBIAN_DIR/stafflog-services $TMP_DIR/$TARBALL_DIR

cd $TMP_DIR/$TARBALL_DIR
dpkg-buildpackage -rfakeroot -us -uc
cd ../..

cp $TMP_DIR/$PACKAGE_FILE $OUT_DIR

rm -rf $TMP_DIR
